﻿#include "loginform.h"
#include "ui_loginform.h"
#include <QMessageBox>
#include <QDebug>

LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginForm)
{
    ui->setupUi(this);
    m_registerForm = new RegisterForm(this);
    connect(m_registerForm,SIGNAL(signalUserRegister(QStringList)),
            this,SIGNAL(signalUserRegisterLogin(QStringList)));
    m_registerForm->hide();
    m_isOkLogin = false;
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::on_pb_register_clicked()
{
    m_registerForm->show();
}

void LoginForm::on_pb_login_clicked()
{
    m_isOkLogin = false;
    bool id =  checkUserID(ui->le_userID->text());
    bool pwd =  checkPassword(ui->le_password->text());

    if ( id && pwd)
    {
        QStringList parameters;
        parameters.append(ui->le_userID->text());
        parameters.append(ui->le_password->text());

        emit signalUserLogin(parameters);
        return;
    }
    QMessageBox::warning(this,
                 "错误",
                 "账号密码输入格式错误!!!",
                 QMessageBox::Ok,
                 QMessageBox::Ok);
}

///匹配英文/数字
bool LoginForm::checkUserID(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]+$");/// \w+  [\u4e00-\u9fa5]{0,}
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkLogin = false;
        }
        else
        {
            m_isOkLogin = true;
        }
    }

    return m_isOkLogin;
}

///匹配英文字符+数字 6-20
bool LoginForm::checkPassword(const QString &arg1)
{
    if (!arg1.isEmpty())
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]{6,20}$");
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkLogin = false;
        }
        else
        {
            m_isOkLogin = true;
        }
    }
    return m_isOkLogin;
}

void LoginForm::FailUserLogin(QString msg)
{
    QMessageBox::warning(this,
                 "错误",
                 msg,
                 QMessageBox::Ok,
                 QMessageBox::Ok);
}

void LoginForm::FailUserRegister(QString msg)
{
    FailUserLogin(msg);
}

void LoginForm::SuccessUserRegister(QString msg)
{
    QMessageBox::warning(this,
                 "成功",
                 msg,
                 QMessageBox::Ok,
                 QMessageBox::Ok);
    m_registerForm->close();
}
